package com.commander;

import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.BasePostprocessor;
import com.facebook.react.bridge.ReadableMap;

import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;

import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.react.views.imagehelper.ImageSource;

import java.text.DecimalFormat;

public class utils {
    private static DecimalFormat fnum = new DecimalFormat("##0.0");

    public static LatLng toLatLng(ReadableMap coordinate){
        return new LatLng(coordinate.getDouble("latitude"), coordinate.getDouble("longitude"));
    }
    public static void fetchImage(View view ,ReadableMap source, Callback callback) {
        // 从 React Native ReadableMap 中获取图片的 URI
//        Log.d("TAG", "fetchImage:"+source.getString("uri"));
        Uri uri = new ImageSource(view.getContext(), source.getString("uri")).getUri();
//        System.out.println(uri);
        // 构建 ImageRequest
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setPostprocessor(new BasePostprocessor() {
                    public void process(Bitmap bitmap) {
                        // 将 Bitmap 转换为 BitmapDescriptor 并执行回调
                        callback.call(BitmapDescriptorFactory.fromBitmap(bitmap));
                    }
                })
                .setResizeOptions(source.hasKey("width") && source.hasKey("height") ?
                        new ResizeOptions(source.getInt("width"), source.getInt("height")) : null)
                .build();

        // 使用 Fresco 的 ImagePipeline 异步加载并处理图片
        DataSource<CloseableReference<CloseableImage>> dataSource = Fresco.getImagePipeline().fetchDecodedImage(request,view);

        // 注册回调以获取加载结果
        dataSource.subscribe(new BaseBitmapDataSubscriber() {
            @Override
            protected void onNewResultImpl(Bitmap bitmap) {
                // 图片加载成功时调用
                if (bitmap != null) {
                    // 将 Bitmap 转换为 BitmapDescriptor 并执行回调
                    callback.call(BitmapDescriptorFactory.fromBitmap(bitmap));
                }
            }

            @Override
            protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                // 图片加载失败时调用
                // 处理加载失败的逻辑
            }
        }, Fresco.getImagePipeline().getConfig().getExecutorSupplier().forBackgroundTasks());
    }

    @FunctionalInterface
    interface Callback {
        void call(BitmapDescriptor bitmapDescriptor);
    }
    public static String getFriendlyTime(int s) {
        String timeDes = "";
        int h = s / 3600;
        if (h > 0) {
            timeDes += h + "小时";
        }
        int min = (int) (s % 3600) / 60;
        if (min > 0) {
            timeDes += min + "分";
        }
        return timeDes;
    }

    public static String getFriendlyDistance(int m) {
        float dis = m / 1000f;
        String disDes = fnum.format(dis) + "公里";
        return disDes;
    }

}



